﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ATFCodeCS
{
	public class Result
	{
		public List<Var> DS { get; set; }
		public List<Config> CF { get; set; }
		public Result()
		{
			DS = new List<Var>();
			CF = new List<Config>();
		}
		public override string ToString()
		{
			StringBuilder str = new StringBuilder();
			foreach (var item in DS)
			{
				str.Append(item.ToString());
				str.Append(Define.CRLF.ToString());
			}
			return str.ToString();
		}
		public void Format()
		{
			this.catNhoNLan();
			this.XuLyDinhDang();
			this.XuLyDinhDang2();
		}
		public string ToStringFormat()
		{
			StringBuilder str = new StringBuilder();
			foreach (var item in DS)
			{
				str.Append(item.ToStringFortmatByID());
			//	str.Append(item._open.ToString());
			//	str.Append(Define.TAB.ToString());
			//	str.Append(item.ID.ToString());
				str.Append(Define.CRLF.ToString());

			}
			return str.ToString();
		}
		#region hàm cắt nhỏ chuỗi
		private void catNho0()
		{
			List<Var> tmp = new List<Var>();
			for (int i = 0; i < DS.Count; i++)
			{
				List<Var> dsn = new List<Var>();

				dsn = DS[i].catDuoi(DS[i]);
				foreach (var j in dsn)
				{
					tmp.Add(j);
				}
			}
			DS = tmp;
		}
		private void catNhon()
		{
			List<Var> tmp = new List<Var>();
			for (int i = 0; i < DS.Count; i++)
			{
				List<Var> dsn = new List<Var>();

				dsn = DS[i].catNhon(DS[i]);
				foreach (var j in dsn)
				{
					tmp.Add(j);
				}
			}
			DS = tmp;
		}

		private void catNho1()
		{
			List<Var> tmp = new List<Var>();
			for (int i = 0; i < DS.Count; i++)
			{
				List<Var> dsn = new List<Var>();

				dsn = DS[i].catDau(DS[i]);
				foreach (var j in dsn)
				{
					tmp.Add(j);
				}
			}
			DS = tmp;
		}
		#endregion

		public void catNhoNLan()
		{
			catNhon();
			catNho0();
			catNho1();
		}
		public void XuLyDinhDang2()
		{
			foreach (var i in DS)
			{
				if (i._open == 1)
					i.ID = i.ID - 1;
			}
		}
		public void XuLyDinhDang()
		{
			int id = 0;
			foreach (Var i in DS)
			{
				foreach (Config j in CF)
				{
					if (j.End.EqualEnd(i))
					{
						id--;
						i.ID = id;
						i._open = -1;
					}
					else if (j.Start.Equals(i))
					{
						i.ID = id ;
						id++;
						i._open = 1;
					}
					else i.ID = id;
				}
			}
		}
		public Result(Text tmp)
		{
			DS = new List<Var>();
			string[] ds = tmp.Data.ToString().Split('\n');
			foreach (var item in ds)
			{
				if (item.ToString().Length > 0)
				{
					DS.Add(new Var(item.ToString()));
				}
			}
		}
	}
}